﻿using System;
using System.Net;
using System.Windows;

namespace FlickrWP7.Core
{
    public class Collections
    {
        private HelperDelegate helperDelegateInstance;

        public string LastCollectionResult { get; set; }

        public void GetCollectionInfo(string apiKey, string signature, string collectionID, HelperDelegate helperDelegate)
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.collections.getInfo&api_key={0}&api_sig={1}&collection_id={2}&format=json",
                    apiKey,signature,collectionID);

            client.DownloadStringAsync(new Uri(URL));
        }

        public void GetCollectionTree(string apiKey, HelperDelegate helperDelegate, string collectionID = "", string userID="")
        {
            helperDelegateInstance = helperDelegate;

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            string URL = string.Format("http://www.flickr.com/services/rest/?method=flickr.collections.getTree&api_key={0}&format=json",
                    apiKey);

            if (collectionID != "")
                URL += "&collection_id=" + collectionID;
            if (userID != "")
                URL += "&user_id=" + userID;

            client.DownloadStringAsync(new Uri(URL));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            LastCollectionResult = e.Result;
            helperDelegateInstance();
        }
    }
}
